package canglang.monitor.core.base.util;


import org.springframework.context.ApplicationContext;


/**
 * The context util class that get spring bean from applicationContext.
 *
 * @author xiaoying
 *
 */
public class ApplicationContextUtil {

		private ApplicationContext applicationContext;
		private static ApplicationContextUtil applicationContextUtil;

	    public static ApplicationContext getApplicationContext(){
	  	   return applicationContextUtil == null ? null :applicationContextUtil.applicationContext ;
	    }
		/**
		 * Set spring applicationContext to hold.
		 * @param applicationContext
		 */
		public static void  setApplicationContext(ApplicationContext applicationContext){
			if(applicationContextUtil == null){
	    		applicationContextUtil =  new ApplicationContextUtil();
	    	}
			applicationContextUtil.applicationContext = applicationContext;
		}

	/**
	 * Get bean from spring applicationContext by bean name.
	 * @param key the name of spring bean in spring configure file.
	 * @return the bean class,if not exists will return null.
	 */
	public static Object getBean(String beanName){
		return getApplicationContext().getBean(beanName);

	}

}

